<?php
/**
 * 
 * 邀请用户
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}


/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","cron_user_invite");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
//$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/mblog/inc.php");
class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		$mblog_from_api = new mblog_from_api();
		if(!$this->start_id)
		{
			$this->start_id=0;
		}
		$sql ="select count(*) as count from user_info where uid > {$this->start_id}";
		$count = $db_mblog_cron_topic_tag->select($sql);
		$count =  $count["count"];
		
		if($count == 0)
		{
			//如果获取不到需要更新的数据，初始化cron_start_id
			$this->ini_start_id(NOW_PROCESS);
			
		}
		
		//由于接口频率限制，只调用一次
		$this->get_list_by_page(1);
		
		
	}	
	
	function get_list_by_page($p)
	{
		$db_mblog_cron_topic_tag = new db_mblog_cron_topic_tag();
		
		$start = ($p-1)*PCOUNT;
		$sql ="select  uid,sex,verified_type from user_info where uid > {$this->start_id} limit $start,".PCOUNT;
		
		$food_tag = $db_mblog_cron_topic_tag->select($sql);
		
		foreach($food_tag as $value)
		{
			//执行时间超过30分钟，退出
			if(time() - CRON_START_TIME > 3600)
			{
				exit;
			}
			$sex = $value["sex"];
			$uid = $value["uid"];
			$verified_type = $value["verified_type"];
			
			if($id % TOTAL_PROCESS != NOW_PROCESS)//用总进程做控制 example: 3个进程  php xxx 1 只会执行除3余1的id
			{
				continue;
			}
			
			
			if($uid)//处理抓取分页逻辑
			{
				$this->get_list_by_topic($uid,$sex,$verified_type);
				
			}
	
			if($uid)//更新抓取的start_id到数据库
			{
				$done_job = true;
				$res = $this->replace_into_data($uid);	
			}
			
			
		}
		
			
	}
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($uid,$sex,$verified_type)
	{
		
		$db_base = new db_base();
		$sql ="select mid,topic ,topic_id from mblog_pic_content where create_uid = {$uid} limit 1 ";
		$res = $db_base->select($sql);
		
		$mid =0;
		if($res)
		{
			$mid = $res[0]["mid"];
			
		}
		else
		{
			$sql ="select mid ,topic,topic_id from mblog_text_content where create_uid = {$uid} limit 1 ";
			$res = $db_base->select($sql);
			if($res)
			{
				$mid = $res[0]["mid"];
			}
		}
		$now = time();
		$sql ="insert ignore into user_invate (uid,mid,create_time,sex) values ($uid,$mid,$now,$sex)";
		$insert_res = @$db_base->query($sql);
		
		if(!$insert_res)
		{
			return false;
		}
		
		if($mid)
		{
			$topic =  $res[0]["topic"];
			$topic_id =  $res[0]["topic_id"];
			
			if($sex ==0)
			{
				if($topic)
				{
					$content = "亲，您好像对{$topic}有兴趣，小编这有个游戏网站推荐给您 http://yxxiu.com/mblog/?topic_id={$topic_id}.html ,有非常多的微博玩家的游戏图片和视频集合，来看看你是否喜欢吧！";
				}
				else
				{
					$content = "亲，小编这有个游戏网站推荐给您 http://yxxiu.com/mblog/ ,有非常多的微博玩家的游戏图片和视频集合，来看看你是否喜欢吧！";
				
				}
			}
			else
			{
				$content = "小编给您推荐一个游戏图片分享站 ,有非常多的微博玩家的游戏图片和视频集合，来看看你是否喜欢吧！ http://yxxiu.com/mblog/ PS:这个链接里还有喜欢打游戏的妹子等着你组队 http://yxxiu.com/want/ ";
			
			}
			
			$admin_uid = 1737886622;
			$db_user_token = new db_user_token();
			$token_info  = $db_user_token->get_token_by_uid_and_appkey($admin_uid,WB_AKEY);
			$token = $token_info["access_token"];
			$SaeTClientV2 = new SaeTClientV2( WB_AKEY , WB_SKEY , $token);
			$res = $SaeTClientV2->send_comment( $mid, $content );
			var_dump($res);
			$cid = $res["id"];
			if($cid)
			{
				$sql = "update user_invate set cid = $cid where uid = $uid";
				@$db_base->query($sql);
			}
			//调用太频繁就退出
			if($res["error_code"] == 10024)
			{
				exit;
			}
		}
		
		
		
		
	}
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
